<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="25%" :title="`${drawerProps.title}-菜单`">
    <el-form ref="ruleFormRef" label-width="100px" label-suffix=":" :rules="rules" :model="operateDataInfo">
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="operateDataInfo.deptName" placeholder="请输入部门名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="父部门" prop="parentId">
        <el-tree-select
          v-model="operateDataInfo.parentId"
          value-key="id"
          :data="[menuTree]"
          :props="{ label: data => data.deptName }"
          check-strictly
          :render-after-expand="false"
          check-on-click-node
          placeholder="请选择父部门"
        />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input type="textarea" v-model="operateDataInfo.remark" placeholder="请描述该部门" :rows="2" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { getSysDeptInfo, getSysDeptTree, SysDeptInfoResult, SysDeptSaveParam, SysDeptUpdateParam } from "@/api/modules/sys/dept";
import { ElMessage, FormInstance } from "element-plus";

interface DrawerProps {
  title: string;
  isSave: boolean;
  isUpdate: boolean;
  row: Partial<SysDeptInfoResult>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isSave: false,
  isUpdate: false,
  title: "",
  row: {}
});

const rules = reactive({
  deptName: [{ required: true, message: "请填写部门名称" }],
  parentId: [{ required: true, message: "请选择父级" }]
});

const operateDataInfo = ref<SysDeptSaveParam | SysDeptUpdateParam>({ deptName: "", parentId: "0" });
const menuTree = ref<SysDeptInfoResult>({ id: "0", parentId: "0", deptName: "根部门", children: [] });

const openDrawer = async (params: DrawerProps) => {
  menuTree.value.children = (await getSysDeptTree()).data;
  if (params.isUpdate) {
    // 更新信息，先查询
    let { data } = await getSysDeptInfo(params.row.id);
    operateDataInfo.value = { ...data };
  } else if (params.isSave) {
    // 新增前的默认值
    operateDataInfo.value = {
      parentId: params.row?.id ? params.row?.id : "0",
      deptName: ""
    };
  }
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(operateDataInfo.value);
      ElMessage.success({ message: `${drawerProps.value.title} 成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  openDrawer
});
</script>

<style scoped lang="scss"></style>
